package com.javaPro.myProject.modules.files.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.UUIDUtil;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.files.entity.TFile;
import com.javaPro.myProject.modules.files.service.TFileService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * (TFile)表控制层
 *
 * @since  22:53:38
 */
@RestController
@RequestMapping("tFile")
public class TFileController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TFileService tFileService;
    @Resource
    private uploadUtil uploadUtil;

    /**
     * 分页查询
     *
     * @param tFile 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(TFile tFile) {
        startPage();
        List<TFile> tFiles = this.tFileService.queryByPage(tFile);
        return  getList("",tFiles);

    }
    @GetMapping("listcourse")
    public ListByPage listcourse(TFile tFile) {
        startPage();
        List<TFile> tFiles = this.tFileService.listcourse(tFile);
        return  getList("",tFiles);

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public ResponseEntity<TFile> queryById(Integer id) {
        return ResponseEntity.ok(this.tFileService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tFile 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(TFile tFile, MultipartFile file) {
        if (file != null){
            String upload = uploadUtil.upload(file);
            tFile.setFilePath(upload);
        }
        return AjaxResult.success(this.tFileService.insert(tFile));
    }

    /**
     * 编辑数据
     *
     * @param tFile 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public AjaxResult edit(TFile tFile) {
        return AjaxResult.autoJudge(this.tFileService.update(tFile));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("del")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.tFileService.deleteById(id));
    }

    /**
     * 文件下载

     */
    @GetMapping("/download")
    @ResponseBody
    public void download(HttpServletResponse response, Integer id){
        TFile tFile = tFileService.queryById(id);
        String filePath = tFile.getFilePath();
        try {
            uploadUtil.downloadLocal(response,filePath, UUIDUtil.getUUID());
            TFile tFile1 = new TFile();
            tFile1.setId(tFile.getId());
            tFile1.setDownloadTimes(1);
            int update = tFileService.update(tFile1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

